package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.model.domain.ModelRelationship;
import org.ediba.siri.core.model.service.ModelService;
import org.springframework.web.bind.annotation.ResponseBody;

public class ModelRelationshipController extends Controller {

	ModelService modelService;
	public void setModelService(ModelService modelService) {this.modelService = modelService;}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ModelRelationshipCommand modelRelationshipCommand){
		List<ModelRelationship> modelRelationships = null;
		int total = 0;
		
		Integer id = null;
		id = modelRelationshipCommand.getId();
		
		if (id!=null){
			modelRelationships = new ArrayList<ModelRelationship>(1);
			modelRelationships.add(this.modelService.relationshipRetrieve(id));
		}else{
			if (modelRelationshipCommand.getSort()==null){
				modelRelationships = this.modelService.relationshipRetrieve();
				total = modelRelationships.size();
				
			}else{
				modelRelationships = this.modelService.relationshipRetrieve(modelRelationshipCommand.getSort(),modelRelationshipCommand.getDir(),modelRelationshipCommand.getStart(),modelRelationshipCommand.getLimit());
				total = this.modelService.relationshipRetrieve().size();
			}
		}
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", modelRelationships);
		_result.put("total", total);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
}
